package com.wqiuqiu.gulimall.member.service.impl;

import com.wqiuqiu.gulimall.member.vo.FareAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.Query;

import com.wqiuqiu.gulimall.member.dao.MemberReceiveAddressDao;
import com.wqiuqiu.gulimall.member.entity.MemberReceiveAddressEntity;
import com.wqiuqiu.gulimall.member.service.MemberReceiveAddressService;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<MemberReceiveAddressEntity> infoByMemberId(Long memberId) {
        return this.list(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberId));
    }

    @Override
    public FareAddressVo getFareAndAddress(Long addrId) {
        FareAddressVo fareAddressVo = new FareAddressVo();
        MemberReceiveAddressEntity addressEntity = this.getById(addrId);
        if (addressEntity != null){
            String phone = addressEntity.getPhone();
            BigDecimal fare = new BigDecimal("0");
            if (StringUtils.isNotEmpty(phone)){
                String substring = phone.substring(phone.length() - 1);
                BigDecimal bigDecimal = new BigDecimal(substring);
                fare = bigDecimal;
            }
            MemberReceiveAddressEntity address = new MemberReceiveAddressEntity();
            BeanUtils.copyProperties(addressEntity,address);
            fareAddressVo.setAddress(address);
            fareAddressVo.setFare(fare);
        }
        return fareAddressVo;
    }


}